package gestioneDatabase.User;

import gestioneDatabase.ElementModifier;
import gestioneDatabase.MultipleDatabaseElementUser;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * Permette la modifica dello stile predefinito da un utente, sostituendolo con un altro.
 *
 */
public class StylesheetModifier extends MultipleDatabaseElementUser {

	private String openIdUser;
	private String newStylesheet;
	private ElementModifier elementModifier = new ElementModifier();

	public StylesheetModifier(String openIdUser, String newStylesheet) {
		super();
		this.openIdUser = openIdUser;
		this.newStylesheet = newStylesheet;
	}

	@Override
	public void execute() {

		try {
			NodeList nList = open("DatabaseUser", "User");

			for (int i = 0; i < nList.getLength(); i++) {

				Node nNode = nList.item(i);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;

					if (eElement.getAttribute("openId").equals(openIdUser)) {

						elementModifier.modify(eElement, newStylesheet,
								"stylesheet");
					}
				}
			}
			controller.writeIntoXMLFile(filepath);

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (SAXException sae) {
			sae.printStackTrace();
		}
	}
}
